package com.ejlchina.cashier.juhe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.ejlchina.cashier.CashReq;
import com.ejlchina.cashier.CashRes;
import com.ejlchina.cashier.Cashier;
import com.ejlchina.cashier.CashierException;


/**
 * 
 * 聚合收款器
 * 
 * @author Troy.Zhou
 *
 */
public class JuheCashier implements Cashier {

	
	Logger log = LoggerFactory.getLogger(JuheCashier.class);
	

	private String bizName;
	
	private CashStarter payStarter;
	
	
	public JuheCashier(String bizName) {
		super();
		this.bizName = bizName;
	}


	@Override
	public void init(CashStarter payStarter) {
		this.payStarter = payStarter;
	}

	
	@Override
	public CashRes cash(CashReq payReq) {
		if (bizName == null) {
			throw new CashierException("bizName can not be null ! please check bizName");
		}
		JSONObject result = payStarter.startPay(bizName, payReq);
		JSONObject head = result.getJSONObject("head");
		JSONObject content = result.getJSONObject("content");
		CashRes payRes = new CashRes();
		payRes.setCode(head.getString("result_code"));
		payRes.setMessage(head.getString("result_msg"));
		if (content != null) {
			payRes.setPayUrl(content.getString("payCode"));
		}
		return payRes;
	}

	@Override
	public String getBizName() {
		return bizName;
	}


	public void setBizName(String bizName) {
		this.bizName = bizName;
	}

}
